<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>成绩管理</title>
    <script src="../js/vue.js"></script>
    <link rel="stylesheet" href="./css/index.css">
</head>


<body>
    <div id="app" class="score-case">
        <div class="table">
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>科目</th>
                        <th>成绩</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item, index) in list" :key="item.id">
                        <td>{{ index + 1 }}</td>
                        <td>{{ item.subject }}</td>
                        <td :class="{ red: item.score < 60 }">{{ item.score }}</td>
                        <td><a href="#" @click.prevent="del(item.id)">删除</a></td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="4">
                            总分：{{ total }} 平均分：{{ average }}
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
        <div class="form">
            <div class="form-item">
                <label for="subject">科目：</label>
                <input type="text" id="subject" v-model.trim="subject" placeholder="请输入科目">
            </div>
            <div class="form-item">
                <label for="score">分数：</label>
                <input type="text" id="score" v-model.number="score" placeholder="请输入分数">
            </div>
            <div class="form-item">
                <button @click="add" class="submit">添加</button>
            </div>
        </div>
    </div>


</body>

</html>
<script>
    const app = new Vue({
        el: '#app',
        data: {
            list: [
                { id: 1, subject: '语文', score: 20 },
                { id: 7, subject: '数学', score: 99 },
                { id: 12, subject: '英语', score: 70 },
            ],
            subject: '',
            score: 0
        },
        computed: {
            total() {
                return this.list.reduce((prev, item) => prev + item.score, 0);
            },
            average() {
                if (this.list.length === 0) {
                    return 0;
                }

                return (this.total / this.list.length).toFixed(2);
            }
        },
        methods: {
            del(id) {
                this.list = this.list.filter(item => item.id !== id);
            },
            add() {
                if (!this.subject || isNaN(this.score)) {
                    alert('请确认您输入的是正确的内容');
                    return;
                }

                this.list.push({
                    id: +new Date(),
                    subject: this.subject,
                    score: parseInt(this.score)
                });

                this.subject = '';
                this.score = 0;
            }
        }
    });
</script>